package yawning.mew.equipment;

import java.util.LinkedHashMap;
import java.util.Map;

public class MemoryCacheItemSource extends ItemSource {
	private static final int SCACHE_MAX_SZ = 100;

	@SuppressWarnings("serial")
	private Map<Integer,RawItem> mCache = new LinkedHashMap<Integer,RawItem>(SCACHE_MAX_SZ, 0.75f, true) {
		protected boolean removeEldestEntry(Map.Entry<Integer,RawItem> eldest) {
			return size() > SCACHE_MAX_SZ;
		}
	};

	@Override
	public RawItem getById(int id) {
		RawItem item;

		// Cache hit
		if (mCache.containsKey(id)) {
			item = mCache.get(id);
			if (item != null) return item;
		}

		// Cache miss
		item = mNext.getById(id);
		if (item != null) mCache.put(id, item);

		return item;
	}
}
